package com.eight.qianTeacher.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.common.pojo.*;
import com.eight.qianTeacher.dao.*;
import com.eight.qianTeacher.service.QianTeacherService;
import com.eight.qianTeacher.vo.ObjVo;
import com.eight.qianTeacher.vo.SubjectVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: qy8-education
 * @description: QianTeacherServiceImpl
 * @author: 韩宗耀
 * @create: 2023-06-02 15:54
 **/
@Service
public class QianTeacherServiceImpl implements QianTeacherService {
    // 讲师
    @Resource
    private EduTeacherDao eduTeacherDao;

    // 课程
    @Resource
    private EduCourseDao eduCourseDao;

    // 课程分类
    @Resource
    private EduSubjectDao eduSubjectDao;

    // 视频
    @Resource
    private EduVideoDao eduVideoDao;

    // 课程章节
    @Resource
    private EduChapterDao eduChapterDao;

    // 课程简介
    @Resource
    private EduCourseDescriptionDao eduCourseDescriptionDao;

    // 课程审核
    @Resource
    private TbCheckCourseDao tbCheckCourseDao;

    @Override
    public EduTeacher findTeacherByMobile(String mobile) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        return eduTeacherDao.selectOne(wrapper);
    }

    @Override
    public Integer updateTeacherByMobile(EduTeacher eduTeacher) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(eduTeacher.getMobile())){
            wrapper.eq("mobile",eduTeacher.getMobile());
        }
        return eduTeacherDao.update(eduTeacher,wrapper);
    }

    @Override
    public IPage<EduCourse> getCourseByTeacherPhone(Integer pageCurrent, Integer pageSize, String mobile, String checkStatus) {
        Page<EduCourse> page = new Page<>(pageCurrent,pageSize);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(mobile)){
            wrapper.eq("mobile",mobile);
        }
        if (StringUtils.hasText(checkStatus)&&!checkStatus.equals("8")){
            wrapper.eq("check_status",checkStatus);
        }
        return eduCourseDao.getCourseByTeacherPhone(page,wrapper);
    }

    @Override
    public Integer changeStatus(EduCourse eduCourse) {
        return eduCourseDao.updateById(eduCourse);
    }



    @Override
    public IPage<EduSubject> findSubjectById(Integer pageCurrent, Integer pageSize, SubjectVo subjectVo) {
        Page<EduSubject> page = new Page<>(pageCurrent, pageSize);
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(subjectVo.getTitle())) {
            wrapper.like("title", subjectVo.getTitle());
        }
        if (subjectVo.getStatusId() != null) {
            wrapper.eq("status_id", subjectVo.getStatusId());
        }
        wrapper.eq("parent_id", "0");
        Page<EduSubject> page1 = eduSubjectDao.selectPage(page, wrapper);
        List<EduSubject> records = page1.getRecords();
        List<EduSubject> list = eduSubjectDao.selectList(null);

        for (EduSubject item : records) {
            List<EduSubject> children = findChildren(item, list);
            item.setChildren(children);
        }
        return page1;
    }

    @Override
    public List<EduSubject> findPidById(String id) {
        return null;
    }

    @Override
    public String updateCourseSubject(ObjVo objVo) {
        // 课程
        EduCourse eduCourse = new EduCourse();
        // 课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        // 课程视频
        EduVideo eduVideo = new EduVideo();
        // 课程审核
        TbCheckCourse tbCheckCourse = new TbCheckCourse();
        // 根据讲师手机获取id
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(objVo.getMobile())){
            wrapper.eq("mobile",objVo.getMobile());
        }
        EduTeacher teacherId = eduTeacherDao.selectOne(wrapper);
        // 课程信息
        eduCourse.setId(IdUtil.getSnowflakeNextIdStr());
        eduCourse.setTeacherId(teacherId.getId());
        eduCourse.setSubjectId(objVo.getCategoryId2());
        eduCourse.setSubjectParentId(objVo.getCategoryId1());
        eduCourse.setTitle(objVo.getTitle());
        eduCourse.setPrice(objVo.getPrice());
        eduCourse.setLessonNum(0);
        eduCourse.setCover(objVo.getCover());
        eduCourse.setBuyCount(0L);
        eduCourse.setViewCount(0L);
        eduCourse.setVersion(1L);
        eduCourse.setStatus("Draft");
        eduCourse.setIsDeleted(0);
        eduCourse.setGmtCreate(LocalDateTime.now());
        eduCourse.setGmtModified(LocalDateTime.now());
        eduCourse.setRemark(objVo.getRemark());
        eduCourse.setIsAdd(0);
        // 课程简介信息
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescription.setDescription(objVo.getDescription());
        eduCourseDescription.setGmtCreate(LocalDateTime.now());
        eduCourseDescription.setGmtModified(LocalDateTime.now());

        // 课程审核信息
        tbCheckCourse.setCourseId(eduCourse.getId());

        eduCourseDao.insert(eduCourse);
        eduCourseDescriptionDao.insert(eduCourseDescription);

        tbCheckCourseDao.insert(tbCheckCourse);

        return eduCourse.getId();
    }

    @Override
    public List<EduChapter> findChapterById(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(courseId)){
            wrapper.eq("course_id",courseId);
        }
        return eduChapterDao.selectList(wrapper);
    }

    @Override
    public List<EduVideo> insertChapterWithCourseId(EduVideo eduVideo) {
        if (StringUtils.hasText(eduVideo.getId())){
            eduVideoDao.updateById(eduVideo);
        }else {
            eduVideo.setId(IdUtil.getSnowflakeNextIdStr());
            eduVideo.setCourseId(eduVideo.getCourseId());
            eduVideo.setChapterId(IdUtil.getSnowflakeNextIdStr());
            eduVideo.setGmtCreate(LocalDateTime.now());
            eduVideo.setGmtModified(LocalDateTime.now());
            eduVideoDao.insert(eduVideo);
        }
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",eduVideo.getCourseId());
        return eduVideoDao.selectList(wrapper);
    }

    @Override
    public Integer deleteChapterWithCourseId(EduChapter eduChapter) {
        eduChapterDao.deleteById(eduChapter);
        return null;
    }

    @Override
    public List<EduVideo> findAllVideo(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(chapterId)){
            wrapper.eq("chapter_id",chapterId);
        }
        return eduVideoDao.selectList(wrapper);
    }

    @Override
    public Integer insertVideo(EduVideo eduVideo) {
        eduVideo.setId(IdUtil.getSnowflakeNextIdStr());
        eduVideo.setGmtCreate(LocalDateTime.now());
        eduVideo.setGmtModified(LocalDateTime.now());
        eduVideoDao.insert(eduVideo);
        return null;
    }

    @Override
    public Integer updateVideo(EduVideo eduVideo) {
        eduVideo.setGmtModified(LocalDateTime.now());
        return eduVideoDao.updateById(eduVideo);
    }

    @Override
    public Integer deleteById(String id, Integer sort, String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(id)){
            wrapper.eq("id",id);
        }
        if (sort!=null){
            wrapper.eq("sort",sort);
        }
        if (StringUtils.hasText(chapterId)){
            wrapper.eq("chapter_id",chapterId);
        }
        eduVideoDao.delete(wrapper);
        return null;
    }


    public List<EduSubject> findChildren(EduSubject eduSubject,List<EduSubject> childrens) {
        ArrayList<EduSubject> list = new ArrayList<>();
        for (EduSubject children : childrens) {
            if(eduSubject.getId().equals(children.getParentId())){
                list.add(children);
            }
        }
        for (EduSubject item : list) {
            eduSubject.setChildren(findChildren(item,childrens));
        }
        return list;
    }
}
